package com.ybb.vue.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ybb.vue.common.Result;
import com.ybb.vue.domain.dto.EpmLineChartDto;
import com.ybb.vue.domain.vo.EpmStatisticsTHistoryDataVo;
import com.ybb.vue.domain.vo.EpmStatisticsTodayDataVo;
import com.ybb.vue.mapper.EpmAlarmNotesMapper;
import com.ybb.vue.mapper.EpmResidentMapper;
import com.ybb.vue.service.EpmAlarmNotesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author ybb
 * @version 1.0
 * @Description  防疫统计controller
 * @Date 2023/3/26 22:06
 */

@RestController
@RequestMapping("/statistics")
@PreAuthorize("hasAuthority('epidemic:prevention:statistics')")
public class EpmStatisticsController {

    @Autowired
    private EpmResidentMapper epmResidentMapper;

    @Autowired
    private EpmAlarmNotesMapper epmAlarmNotesMapper;

    @Autowired
    private EpmAlarmNotesService epmAlarmNotesService;

    /**
     * 获取今日居民人数、健康码异常人数、异常率
     * @param
     * @return
     */
    @GetMapping("/todayData")
    public Result<?> todayData(){
        EpmStatisticsTodayDataVo epmStatisticsTodayDataVo = new EpmStatisticsTodayDataVo();
        // 居民人数
        epmStatisticsTodayDataVo.setResidentToalToday(epmResidentMapper.selectList(new QueryWrapper<>(null)).size());
        // 健康码异常人数
        epmStatisticsTodayDataVo.setExpToday(epmAlarmNotesMapper.selectExpResidentToday().size());
        // 异常率
        epmStatisticsTodayDataVo.setExpRateToday((double)epmStatisticsTodayDataVo.getExpToday() / epmStatisticsTodayDataVo.getResidentToalToday() * 100);
        return Result.success(epmStatisticsTodayDataVo);
    }

    /**
     * 获取历史居民人数、健康码异常人数、异常率
     * @param
     * @return
     */
    @GetMapping("/historyData")
    public Result<?> historyData(){
        EpmStatisticsTHistoryDataVo epmStatisticsTHistoryDataVo = new EpmStatisticsTHistoryDataVo();
        // 居民人数
        epmStatisticsTHistoryDataVo.setResidentToalHistory(epmResidentMapper.selectList(new QueryWrapper<>(null)).size());
        // 健康码异常人数
        epmStatisticsTHistoryDataVo.setExpHistory(epmAlarmNotesMapper.selectExpResidentHistory().size());
        // 异常率
        epmStatisticsTHistoryDataVo.setExpRateHistory((double)epmStatisticsTHistoryDataVo.getExpHistory() / epmStatisticsTHistoryDataVo.getResidentToalHistory() * 100);
        return Result.success(epmStatisticsTHistoryDataVo);
    }

    /**
     * 查询今日数据的折线图数据
     */
    @PostMapping("/lineChartToday")
    public Result<?> lineChartToday(@RequestBody EpmLineChartDto epmLineChartDto) {
        return Result.success(epmAlarmNotesService.getTodayLine(epmLineChartDto));
    }

    /**
     * 查询历史数据的折线图数据
     */
    @PostMapping("/lineChartHistory")
    public Result<?> lineChartHistory(@RequestBody EpmLineChartDto epmLineChartDto) {
        return Result.success(epmAlarmNotesService.getHistoryLine(epmLineChartDto));
    }
}
